package lormi.meipin.common;

import lormi.meipin.exception.LMException;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;

/*
 * 微信api
 * 
 * */
public class LMWeiXinAPI {
	
	private static final String TOKEN = "MSt1NIB3jcY5ah7T4Cgz";//接收推送token
	private static final String APP_KEY = "wx231d6e82d441304f";//app 的 key
	private static final String APP_SECRET = "7ffe796be32c10daad6db1ae9ace323a";//app 的 secret
	private static final String APP_AESKEY = "MStvNIBCjcYeahiTJCgzZtgC9kuuWGnIwmdtCldPPWW";//聊天内容加密密钥
	
	private static final WxMpService wxMpService = new WxMpServiceImpl();
	private static final WxMpInMemoryConfigStorage config = new WxMpInMemoryConfigStorage();
	/**
	 * 获取微信带参数的二维码地址
	 */
	public static String getQrCode(Integer code) throws LMException{
		config.setAppId(APP_KEY); // 设置微信公众号的appid
		config.setSecret(APP_SECRET); // 设置微信公众号的app corpSecret
		config.setToken(TOKEN); // 设置微信公众号的token
		config.setAesKey(APP_AESKEY); // 设置微信公众号的EncodingAESKey

		wxMpService.setWxMpConfigStorage(config);
		
		WxMpQrCodeTicket ticket;
		//零时ticket
		try {
			ticket = wxMpService.qrCodeCreateTmpTicket(code, 2592000);
			if(ticket!=null){
				return ticket.getUrl();
			}
		} catch (WxErrorException e) {
			throw new LMException();
		}
		return "";
	}
	
}
